import Vue from 'vue'
import MessageBoxVue from './index.vue'

const BoxConstructor = Vue.extend(MessageBoxVue)
let dom : any
const messageBox = {
  show: (data:{
    title?: string,
    content?: string,
    confirmText?: string,
    duration: number
    cancelText?: string,
    showCancel?: boolean,
    confirm?: {():void}
    cancel?: {():void}
  }) => {
    dom = new BoxConstructor({
      el: document.createElement('template'),
      data () {
        return data
      }
    })
    document.body.appendChild(dom.$el)
  },
  hide: () => {
    if (dom !== undefined) document.body.appendChild(dom.$el)
  }
}

const sleep = (time : number) => {
  return new Promise(resolve => setTimeout(resolve, time))
}

function install () : void {
  Vue.prototype.$messageBox = messageBox
  Vue.prototype.$sleep = sleep
}

export default install
